﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine.SceneControl;
using PloobsEngine.Modelo;
using PloobsEngine.Physics.Bepu;
using Microsoft.Xna.Framework;
using PloobsEngine.Material;
using PloobsEngine.IA;

namespace Etapa1.Pedestrian
{
    public class FollowPathAstar : IAgent
    {
        SimpleObject so;
        ObjectMover om;
        LinkedList<Waypoint> ways;
        public FollowPathAstar(LinkedList<Waypoint> ways)
        {
            this.ways = ways;
            SimpleModel sm = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\caixaTex");
            sm.LoadModelo();
            BoxObject pi = new BoxObject(ways.First.Value.WorldPos, 1, 10, new Vector3(3));
            om = new ObjectMover(pi);

            IShader shader = new NormalDeferred();
            shader.Initialize();
            IMaterial mat = new Material(shader, MaterialType.DEFERRED);
            so = new SimpleObject(mat, om, sm);
            so.Agente = this;
        }


        bool ended = false;
        bool firstTime = true;
        #region IAgent Members

        public void Update(GameTime gt)
        {
            if (ended == true)
                return;

            if(firstTime == true)
            {
                
                om.Mover.Entity.IsAlwaysActive = true;
                om.Mover.Entity.IsActive = true;                
                om.Mover.TargetPosition = ways.First.Value.WorldPos;
                ways.RemoveFirst();
                firstTime = false;
            }

            om.Mover.TargetPosition = ways.First.Value.WorldPos;
            om.Mover.IsUpdating = true;
            om.Mover.LinearMotor.Settings.MaximumForce = 70;

            if ((so.Position - ways.First.Value.WorldPos).Length() < 15)
            {
                ways.RemoveFirst();
                if (ways.First == null)
                {
                    ended = true;
                    return;
                }
                om.Mover.TargetPosition = ways.First.Value.WorldPos;
            }            

        }

        public IObject Obj
        {
            get
            {
                return so;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
